//
//  MainChartAVGSimpleView.m
//  ChartRefactor
//
//  Created by CaoJun on 15/3/25.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#import "MainChartAVGView.h"

using namespace ytx;

@interface MainChartAVGView()

@end

@implementation MainChartAVGView

-(void)constructor
{
    [super constructor];
    
    [self addSubview:self.rectPainterView];
    
    [self addSubview:self.userAVGPainterView];
    
    [self addSubview:self.linePainterView];
    
    [self addSubview:self.textPainterView];
    
    [self addSubview:self.crosshairsPainterView];
    
    [self addSubview:self.userPainterView];
    
    self.rectPainterView.indexPosition = self.indexPosition;
    
    self.userAVGPainterView.indexPosition = self.indexPosition;
    
    self.linePainterView.indexPosition = self.indexPosition;
    
    self.textPainterView.indexPosition = self.indexPosition;
    
    self.crosshairsPainterView.indexPosition = self.indexPosition;
    
    self.userPainterView.indexPosition = self.indexPosition;

}

-(void)render:(AVGData *) data
{
    if (data == NULL) {
        return;
    }
    [self resize];

    self.avgData = data;
    
    self.indexPosition->initViewIndexByDataIndexAndRect(data, data->getDrawingCount(), 0, data->getDrawingCount());
    
    [self paint];
}

-(void)renderKeepIndexPosition:(AVGData *) data
{
    if (data == NULL) {
        return;
    }
    
    if(!self.indexPosition->hadInited() || data != self.avgData  ){
        self.indexPosition->initViewIndexByDataIndexAndRect(data, data->getDrawingCount(), Config::getInstance()->kLine.min, Config::getInstance()->kLine.max);
    }
    [self resize];
    self.indexPosition->concatCount(data);
    self.indexPosition->setPara(data);
    self.avgData = data;
    [self paint];
}

-(void)render
{
    if (![self hasData]) {
        if(self.avgData != NULL){
            [self.rectPainterView paint: self.indexPosition withPainter: [PainterRectAVG new] ];
        }
        return;
    }
    [self resize];
    self.indexPosition->getRectangle().resize(self.bounds.size.width, self.bounds.size.height);
    self.indexPosition->setPara(self.avgData);
    [self paint];
}

-(void)paint{
    [self.rectPainterView paint: self.indexPosition withPainter: [PainterRectAVG new] ];
    [self.userAVGPainterView paint:self.indexPosition withPainter: [PainterUserAVG new] ];
    [self.linePainterView paint: self.indexPosition withPainter: [PainterLineAVG new] ];
    [self.textPainterView paint: self.indexPosition withPainter: [PainterTextAVG new] ];
    [self.userPainterView paint: self.indexPosition withPainter: [PainterUserAVGData new] ];
    [super paint];
}

- (BOOL) hasData
{
    return self.avgData != NULL && self.avgData->hasData();
}

- (void) clear
{
    [self.rectPainterView clear];
    [self.userAVGPainterView clear];
    [self.linePainterView clear];
    [self.textPainterView clear];
    [self.userPainterView clear];
    [super clear];
}

- (void) drawCrosshairsWithX:(NSNumber *) dx withY:(NSNumber *) dy
{
    if (self.crosshairsPainterView != nil) {
        [self.crosshairsPainterView drawCrosshairs:self.indexPosition withPainter: [PainterCrosshairsAVG new] withX:dx withY:dy];
    }
}

- (void) drawCrosshairsWithX:(NSNumber *) dx
{
    if (self.crosshairsPainterView != nil) {
        [self.crosshairsPainterView drawCrosshairs:self.indexPosition withPainter: [PainterCrosshairsAVG new] withX:dx];
    }
}

- (void) drawCrosshairsAndBottomTextWithX:(NSNumber *) dx withY:(NSNumber *) dy
{
    if (self.crosshairsPainterView != nil) {
        [self.crosshairsPainterView drawCrosshairs:self.indexPosition withPainter: [PainterCrosshairsAVG new] withX:dx withY:dy isDrawBottomText:YES];
    }
}

@end
